package com.toolbox.service.impl;

import com.toolbox.bo.AccountBO;
import com.toolbox.dal.repository.InfraMenuDORepository;
import com.toolbox.dal.repository.InfraRoleMenuDORepository;
import com.toolbox.dto.InfraMenuDTO;
import com.toolbox.req.InfraMenuReq;
import com.toolbox.service.InfraMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.toolbox.constant.Constant.DEFAULT_MANAGE_ROLE_ID;

/**
 * <p>
 * 菜单权限 服务实现类
 * </p>
 *
 * @author Mango
 * @since 2023-12-01
 */
@SuppressWarnings("ALL")
@Slf4j
@Service
public class InfraMenuServiceImpl implements InfraMenuService {

    @Autowired
    private InfraMenuDORepository repository;

    /**
     * 角色菜单表 服务类
     */
    @Autowired
    private InfraRoleMenuDORepository infraRoleMenuDORepository;

    @Override
    public List<InfraMenuDTO> getMenuNodes() {
        return repository.getMenuNodes();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Long createMenu(InfraMenuReq req, AccountBO bo) {
        // 1、创建——菜单权限
        Long menuId = repository.createMenu(req, bo);

        // 2、创建角色与菜单关系
        infraRoleMenuDORepository.save(DEFAULT_MANAGE_ROLE_ID, menuId, bo);

        return menuId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Long updateMenuById(InfraMenuReq req, AccountBO bo) {
        return repository.updateMenuById(req, bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void removeById(Long id) {
        // 1、删除——菜单权限
        repository.removeById(id);

        // 2、根据角色ID删除菜单与角关系
        infraRoleMenuDORepository.removeByMenuId(id);
    }

}
